﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YAPL.Language.Ast.Transformation {
	public class AstOptimizer {
		private readonly List<IAstTransform> _transformations = new List<IAstTransform> {new OperatorPrecomputationTransform(), new FunctionPrecomputationTransform() };

		public Expression Optimize(Expression source) {

			var treeModified = true;

			while (treeModified) {

				treeModified = false;
				foreach (var transformation in _transformations) {
					bool modifiedByOne;
					source = transformation.Transform(source, out modifiedByOne);
					treeModified = treeModified || modifiedByOne;
				}
			}
			return source;
		}
	}
}
